package slap.mvc;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import slap.app.Application;
import slap.exception.SlapException;
import slap.exception.UnexpectedException;
import slap.http.Request;
import slap.http.websocket.Inbound;
import slap.http.websocket.Outbound;
import slap.mvc.result.Forbidden;
import slap.mvc.result.NotFound;

public class WebSocketInvoker {
	protected static final Logger logger = LoggerFactory.getLogger(WebSocketInvoker.class);
    public static void invoke(Request request, Inbound inbound, Outbound outbound) {

        try {
            Application app = ActionInvoker.resolveApplication(request);
            String path = request.path();
            WebSocketHandler handler = app.webSocketHandlersMapping.get(path);
            if(handler == null){
            	throw new NotFound(path);
            }
            Inbound.current().handler = handler;
            inbound.handler.onOpen();
        }catch (SlapException e) {
            throw e;
        } catch (Exception e) {
            throw new UnexpectedException(e);
        }

    }
}
